package vis.core.common;

import vis.core.domain.sys.MSG;
import vis.core.domain.sys.SRS;
import vis.core.domain.sys.ServiceException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Configuration
@RestControllerAdvice
public class ExceptionHandler {


    @org.springframework.web.bind.annotation.ExceptionHandler(value = Throwable.class)
    public SRS<Object> exceptionHandler(Throwable ex) {
        log.error("【Throwable】", ex);
        return SRS.byError();
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(value = NotLoginException.class)
    public SRS<Object> serviceExceptionHandler(NotLoginException ex) {
        log.error("【NotLoginException】", ex);
        return SRS.byError(MSG.MAST_LOGIN);
    }


    @org.springframework.web.bind.annotation.ExceptionHandler(value = ServiceException.class)
    public SRS<Object> serviceExceptionHandler(ServiceException ex) {
        log.error("【ServiceException】", ex);
        return ex.getResponse();
    }

    //接收参数转换异常
    @org.springframework.web.bind.annotation.ExceptionHandler(value = HttpMessageNotReadableException.class)
    public SRS<Object> httpMessageExceptionHandler(HttpMessageNotReadableException ex) {
        log.error("【HttpMessageNotReadableException】", ex);
        return SRS.byError(MSG.PARAM_ER);
    }

    //接收参数转换异常
    @org.springframework.web.bind.annotation.ExceptionHandler(value = MissingServletRequestParameterException.class)
    public SRS<Object> paramExceptionHandler(MissingServletRequestParameterException ex) {
        log.error("【MissingServletRequestParameterException】", ex);
        return SRS.byError(MSG.PARAM_ER);
    }

    //权限不足-缺角色
    @org.springframework.web.bind.annotation.ExceptionHandler(value = NotRoleException.class)
    public SRS<Object> paramExceptionHandler(NotRoleException ex) {
        log.error("【MissingServletRequestParameterException】", ex);
        return SRS.byError(MSG.ER_403);
    }

    //权限不足-缺细粒度权限
    @org.springframework.web.bind.annotation.ExceptionHandler(value = NotPermissionException.class)
    public SRS<Object> paramExceptionHandler(NotPermissionException ex) {
        log.error("【MissingServletRequestParameterException】", ex);
        return SRS.byError(MSG.ER_403);
    }


}
